<?php
/**
 * Created by PhpStorm.
 * Script Name: Follow.php
 * Create: 2022/10/31 8:32
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\mp\event;


use app\common\event\Base;
use app\common\model\MpFollow;

class Follow extends Base
{
    /**
     * @var MpFollow
     */
    private $followM;

    public function __construct()
    {
        parent::__construct();
        $this->followM = new MpFollow();
    }

    /**
     * 保存粉丝数据
     * @param array $original
     * @param int $mp_id
     * @return array|bool|false|mixed|\PDOStatement|string|\think\Model
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function saveFollow($original = [], $mp_id = 0){
        $original_data = [
            'mpid'          => $mp_id,
            'openid'        => $original['openid'],
            'nickname'      => $original['nickname'],
            'sex'           => $original['sex'],
            'headimgurl'    => $original['headimgurl'],
            'language'      => $original['language'],
            'city'          => $original['city'],
            'province'      => $original['province'],
            'country'       => $original['country'],
            'unionid'       => $original['unionid'] ?? '',
            'subscribe_time' => $original['subscribe_time'] ?? 0,
            'subscribe_scene' => $original['subscribe_scene'] ?? ''
        ];

        if(! $follow = $this->followM->getOneByMap(['mpid' => $mp_id, 'openid' => $original_data['openid']])){
            $follow = $this->followM->addOne($original_data);
        }
        return $follow;
    }
}